﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims; // 添加添加此命名空间引用

namespace WebApplication1.Controllers;

[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    /// <summary>
    /// 公开接口
    /// </summary>
    [HttpGet("public")]
    public IActionResult GetPublic()
    {
        return Ok(new { message = "这是公开信息" });
    }

    /// <summary>
    /// 受保护接口（需微信授权）
    /// </summary>
    [Authorize]
    [HttpGet("info")]
    public IActionResult GetUserInfo()
    {
        // 从JWT中获取用户信息（此时ClaimTypes已可正常使用）
        var openid = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        var nickname = User.FindFirst(ClaimTypes.Name)?.Value;
        var headimgurl = User.FindFirst("Headimgurl")?.Value;

        return Ok(new { openid, nickname, headimgurl });
    }
}